<?php
$start = microtime(true);
error_reporting(E_ALL);
set_time_limit(0);
ini_set('memory_limit', '512M');

include '.././conf/config.php';
include '.././helper/helper.php';
include 'curl.php';

$remote = new cUrl();
$doc = new DOMDocument();

$nextUrl = 'http://www.alim.org/library/hadith/SHB/1/1';
$handle = fopen('csv/bukhari.csv', 'w');
fputcsv($handle, array('hadith_number', 'volume', 'book', 'narrator', 'full_text'));
$retry = 0;
while (true) {
    echo $nextUrl . "\n";
    $arr = explode('/', $nextUrl);
    $volume = array_pop($arr);
    $hadithNumber = array_pop($arr);
        
    $remote->url = $nextUrl;
    $outPut = $remote->exe();
    if (!$outPut) {
        $retry++;
        echo "\n" . 'Failed  ... retrying ' . $retry;
        if ($retry == 5) { echo "\n" . ' shutting down ';           break; }
        continue;
    }
    $retry = 0;
    
    
    @$doc->loadHTML($outPut);
    
    $hadithInfoBlock = $doc->getElementById('recitation_hadith');
    if (!$hadithInfoBlock) {        continue; }
    $table = $hadithInfoBlock->getElementsByTagName('table')->item(0);
    
    $trNarrator = $table->getElementsByTagName('tr')->item(7);
    $narrator = $trNarrator->getElementsByTagName('a')->item(0)->nodeValue;
    $bookName = $table->getElementsByTagName('tr')->item(9)->nodeValue;
    
    $contentWrapper = $doc->getElementById('content_data');
    $divs = $contentWrapper->getElementsByTagName('div');
    foreach ($divs as $div) {
        if ($div->getAttribute('class') == 'field-content') {
            $fullText = $div->nodeValue;
        }
    }
    $data = array(trim($hadithNumber), trim($volume), trim($bookName), trim($narrator), trim($fullText));
    fputcsv($handle, $data);
    
    $nextButtonParent = $doc->getElementById('con_page');
    $images = $nextButtonParent->getElementsByTagName('img');
    
    $i = 0;
    foreach ($images as $image) {
        if ($image->getAttribute('src') == 'http://www.alim.org/sites/all/themes/alim/images/next3.jpg') {
            $a = $image->parentNode;
            $nextUrl =  'http://www.alim.org' . $a->getAttribute('href');
            $i++;
        }
    }
    
    if ($i == 0) {
        break;
    }
}


fclose($handle);

echo 'done ...........' . "\n";
echo microtime(true) - $start;